package com.chandra.pos.customer.repository;

import com.chandra.pos.customer.dao.entity.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Customer: T4671
 * Date: 10/1/13
 * Time: 4:34 PM
 * To change this template use File | Settings | File Templates.
 *
 * http://spring.io/blog/2011/02/10/getting-started-with-spring-data-jpa/
 */

@Transactional(readOnly = true)
public interface CustomerRepository extends JpaRepository<Customer, Long> ,CustomerRepositoryCustom{


    @Query("select u from Customer u where lower(u.firstName) = ?1 and lower(u.firstName) = ?2 and lower(u.firstName) = ?3")
    public Customer findByFirstNameAndLastNameAndCity(String firstName, String lastName,  String city);

    @Modifying
    @Query("update Customer u set u.isSupplier = ?1 where u.id = ?2")
    int markCustomerAsSupplier(Boolean supplier, Long customerId);

    @Query("select u from Customer u where lower(u.firstName) like  :query or lower(u.lastName) like   :query or lower(u.nickName) like   :query")
    public List<Customer> search(@Param("query") String  query);
}
